package com.strelitzia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.strelitzia.dao.NotificationDao;
import com.strelitzia.entity.DynamicEntity;
import com.strelitzia.entity.NotificationEntity;
import com.strelitzia.entity.ReviewEntity;
import com.strelitzia.entity.UserEntity;
import com.strelitzia.filter.CorsFilter;
import com.strelitzia.service.DynamicService;
import com.strelitzia.service.NotificationService;
import com.strelitzia.service.ReviewService;
import com.strelitzia.service.UserService;
import com.strelitzia.utils.TimeUtil;
import com.strelitzia.vo.NotificationItem;
import com.strelitzia.vo.VisitorItem;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @create 2024/1/16 11:10
 */
@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationDao, NotificationEntity> implements NotificationService {

    @Autowired
    UserService userService;

    @Autowired
    ReviewService reviewService;
    @Autowired
    DynamicService dynamicService;

    @Override
    public List<NotificationItem> getList() {
        NotificationEntity entity = new NotificationEntity();
        entity.setNoRead(1);
        this.update(entity, new UpdateWrapper<NotificationEntity>().eq("receiver", CorsFilter.loginUserId.get()));
        List<NotificationEntity> list = this.list(new QueryWrapper<NotificationEntity>()
                .eq("receiver", CorsFilter.loginUserId.get())
                .orderByDesc("time"));
        if (list != null) {
            return list.stream().map(item -> {
                NotificationItem notificationItem = new NotificationItem();
                notificationItem.setUid(item.getNotifier());

                if(item.getNotifier() != 0) {
                    UserEntity byId = userService.getById(item.getNotifier());
                    notificationItem.setAvatar("/api/res/" + byId.getAvatar());
                    notificationItem.setNickname(byId.getNickname());
                } else {
                    notificationItem.setAvatar("/api/res/logo.png");
                    notificationItem.setNickname("系统");
                }
                notificationItem.setTime(TimeUtil.formattingTime(item.getTime()));

                notificationItem.setMessage(item.getMessage());
                switch (item.getType()) {
                    case 0:
                        DynamicEntity byId1 = dynamicService.getById(item.getAppend());
                        if (byId1 != null){
                            notificationItem.setNotification(byId1.getContent());
                        }
                        else {
                            notificationItem.setNotification("该动态已删除");
                        }
                        break;
                    case 1:
                        ReviewEntity byId2 = reviewService.getById(item.getAppend());
                        if (byId2 != null){
                            notificationItem.setNotification(byId2.getContent());
                        }
                        else {
                            notificationItem.setNotification("该评论已删除");
                        }
                        break;
                }

                return notificationItem;
            }).collect(Collectors.toList());
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public List<VisitorItem> getVisitor() {
        List<NotificationEntity> list = this.list(new QueryWrapper<NotificationEntity>()
                .eq("receiver", CorsFilter.loginUserId.get())
                .eq("type", 2)
                .orderByDesc("time").groupBy("notifier"));
        if (list != null) {
            return list.stream().map(item -> {
                VisitorItem visitorItem = new VisitorItem();
                UserEntity byId = userService.getById(item.getNotifier());
                visitorItem.setAvatar("/api/res/" +byId.getAvatar());
                visitorItem.setNickname(byId.getNickname());
                visitorItem.setId(byId.getId());
                return  visitorItem;
            }).collect(Collectors.toList());
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public NotificationEntity getFriend(Integer notifier, Integer receiver) {
        return this.baseMapper.getFriend(notifier, receiver);
    }

}
